package com.clinic.services.imp;

import java.util.List;

import javax.ws.rs.Path;

import com.clinic.dao.imp.Patient_imp;
import com.clinic.domain.Patient;
import com.clinic.services.interfaces.Patient_services;
import com.clinic.util.Util_imp;
import com.fasterxml.jackson.databind.ObjectMapper;

@Path("/patientservices")
public class Patient_services_imp implements Patient_services {

	@Override
	public String get_patient(int id) {
		System.out.println("Enter - get_patient service");
		Patient p = new Patient_imp().get_patient(id);
		System.out.println("Exit - get_patient service");
		if(p != null){
			return Util_imp.to_json(p);
		} else {
			return "";
		}
	}

	@Override
	public String get_patients() {
		System.out.println("Enter - get_patient service");
		List<Patient> p = new Patient_imp().get_all_patients();
		System.out.println("Exit - get_patient service");
		if(p.size() > 0){
			return Util_imp.to_json(p);
		} else {
			return "";
		}
	}

	@Override
	public String create_patient(String jsonobj) {
		System.out.println("Enter - create_patient service");
		String returned_value = "";
		ObjectMapper mapper = new ObjectMapper();
		Patient p;
		try {
			p = mapper.readValue(jsonobj, Patient.class);
			boolean check = new Patient_imp().is_patient_exist(p.getEmail_address());
			if(check == false){
				String token = new Patient_imp().create_patient(p);
				if(token != ""){
					returned_value = "Valid&"+token;
				}
			} else {
				returned_value = "Found";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("Exit - create_patient service");
		return returned_value;
	}
	
	@Override
	public String signin(String jsonfile) {
		System.out.println("Enter - Login method ");
		String result="notValid";
		ObjectMapper mapper = new ObjectMapper();
		Patient p = null;
		try{
			p = mapper.readValue(jsonfile, Patient.class);
			System.out.println("email " + p.getEmail_address() + " passwd " + p.getPasswrd());
		}catch(Exception ex){
			ex.printStackTrace();
		}
		Patient returned = new Patient_imp().signin(p.getEmail_address(), p.getPasswrd());
		if(returned != null){
			result = "Valid";
		}
		System.out.println("Exit - Login method ");
		return result;
	}

	@Override
	public String validate(String jsonfile) {
		System.out.println("Enter - Validate method");
		ObjectMapper mapper = new ObjectMapper();
		Patient p;
		String returned_value = "";
		try {
			p = mapper.readValue(jsonfile, Patient.class);
			
			boolean result = new Patient_imp().is_token_valid(p.getEmail_address(), p.getToken());
			if(result){
				returned_value = "Valid";
			} else {
				returned_value = "Failed";
			}
			System.out.println("Exit - Validate method");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returned_value;
	}

	@Override
	public String get_patientname(String email) {
		System.out.println("Enter - get_patientname");
		String full_name = new Patient_imp().get_patient(email);
		System.out.println("Exit - get_patientname");
		return full_name;
	}
}